/**
 * @description 选择排序
 */
Array.prototype.select = function(){
    for(let i = 0 ; i < this.length-1 ; i++ ){
        for(let j = i+1 ; j < this.length ; j++ ){
             if(this[i] > this[j]){
                  let temp = this[i]
                  this[i] = this[j]
                  this[j] = temp
             }
        }
    }
}

// 二元选择排序
Array.prototype.binarySelect = function(){
    let leng = this.length
    for(let i = 0 ; i <= leng*0.5 ; i++ ){
        let temp;
        let leftIndex = i ;                 // 左边界
        let rightIndex = leng -1 -i        // 右边界
        let minIndex = i                  // 内层遍历最大数组的索引
        let maxIndex = i                   // 内层遍历最小数组的索引

        for(let j = leftIndex + 1 ; j<= rightIndex ; j++){
              if(this[j] > this[maxIndex] )  maxIndex = j;
              if(this[j] < this[minIndex])   minIndex = j;
        }
     
        // 最大最小数组交换到边界位置
        temp = this[minIndex];
        this[minIndex] = this[leftIndex];
        this[leftIndex] = temp

        temp = this[maxIndex];
        this[maxIndex] = this[rightIndex];
        this[rightIndex] = temp;
    }
}


let arr = [1,2,50,3,5,44,0,-4];
arr.binarySelect();
console.log(arr);